<template>
    <div class="ChangeTable">

        <Row>
            <Col span="24">
                <Button type="primary" class="export-btn" @click="add">新增</Button>
            </Col>
            <Table :loading="loading" border :columns="columns" :data="data" width="100%">
                <!--                <template slot-scope="{ row }" slot="user">-->
                <!--                    <span>{{setProblemTypeTitle(formValidate.userRealName)}}</span>-->
                <!--                </template>-->
                <template slot-scope="{ row, index }" slot="costType">
                    <span>{{getTitle('exemptFeeType',row.costType)}}</span>
                </template>

                <template slot-scope="{ row }" slot="action">

                    <Button type="primary" size="small" @click="edit(row)"
                    >编辑
                    </Button>
                    <Button type="error" size="small" @click="remove(row)">
                        删除
                    </Button>
                </template>
            </Table>
        </Row>
        <Row type="flex" justify="end" class="page">
            <Page :current="formValidate.pageNumber" :total="total" :page-size="formValidate.pageSize"
                  @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]"
                  size="small" show-total show-elevator show-sizer></Page>
        </Row>
        <addUserToExempt
            :isModalOpen="isModalOpen"
            :modalTitle="modalTitle"
            :baseData="rowParams"
            @closeModal="closeModal"
            :userData="userData"
            :dataDict="dataDict"
            ref="addUserToExempt"
            :width="500"
        />
    </div>
</template>
<script>
    import {
        getConfigListData,
        deleteExemptConfig,
    } from "@/projects/basic/api/contract/fee-waiver/exempt-configuration/index";
    import {
        getAllUserData
    } from "@/projects/basic/api/admin/user";
    import {getTitleFromDataDict} from "_p/basic/assets/contract/js/utils";
    import {getDictDataList} from "_p/basic/assets/contract/js/utils";
    import addUserToExempt from "./addUserToExempt";

    export default {
        name: "user-configuration",
        components: {
            addUserToExempt,
        },
        data() {
            return {
                userData: [],
                data: [],
                lprModalVisible: false,
                loading: true,
                total: 0,
                userData: [],
                dicttArr: ['exemptFeeType'],
                exemptFeeTypeDict: [],
                dataDict: {}, // 数据字典
                rowParams: {
                    id: '',
                    username: '',
                    limitAmt: null,
                    edit: true,
                },
                openModal: false,
                isModalOpen: false,
                pageNo: 1,
                modalTitle: '',
                formValidate: {
                    username: '',
                    limitAmt: null,
                    userRealName: '',
                    pageNumber: 1,
                    pageSize: 10,
                },
                columns: [
                    {
                        title: "操作",
                        slot: "action",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "用户",
                        key: "userRealName",
                        align: "center",
                        // slot: "user"
                    },
                    {
                        title: "费用类型",
                        key: "costType",
                        type: "text",
                        align: "center",
                        slot: "costType",
                    },
                    {
                        title: "单笔合同最高限额",
                        key: "limitAmt",
                        align: "center"
                    },

                ],
            }
        },
        computed: {},
        mounted() {
            this.init();
        },
        methods: {
            init() {
                this.getList();
                this.getAllUserData();
                this.getDictDataList();
            },

            getList() {
                this.loading = true;
                getConfigListData(this.formValidate).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        this.total = res.data.total;
                        let dateList = res.data.records;
                        getAllUserData().then(result => {
                            if (result.code === "0000") {
                                for (let i = 0; i < dateList.length; i++) {
                                    for (let j = 0; j < result.data.length; j++) {
                                        if (dateList[i].user === result.data[j].username) {
                                            dateList[i].userRealName = result.data[j].userRealName;
                                            // this.data.push(dateList[i]);
                                        }
                                    }
                                }
                                console.log(dateList, "dateList")
                                this.data = dateList
                            }
                        });
                        console.log(this.data);
                    }
                });
            },
            getTitle(type, val) {
                let data = this.dataDict[type + "Dict"] || [];
                return getTitleFromDataDict(data, val);
            },
            getDictDataList() {
                getDictDataList(this.dicttArr).then(res => {
                    console.group("数据字典:");
                    console.log(this.dicttArr.join("  "));
                    console.log(JSON.parse(JSON.stringify(res)));
                    console.groupEnd();
                    this.dataDict = res;
                });
            },
            getAllUserData() {
                getAllUserData().then(res => {
                    if (res.code === "0000") {
                        console.log("res.data", res.data)
                        this.userData = res.data
                    }
                })
            },
            changePageSize(v) {
                this.formValidate.pageSize = v;
                this.getList();
            },
            changePage(v) {
                this.formValidate.pageNumber = v;
                this.getList();
            },
            // 弹窗

            // showModal(params) {
            //     console.log("params", params)
            //     if (params !== null) {
            //         this.rowParams = params
            //         this.rowParams.username = this.rowParams.user
            //         this.modalTitle = '编辑'
            //         this.isModalOpen = true;
            //     }else {
            //
            //     }
            //
            //
            // },
            add() {
                this.rowParams.id = '';
                this.rowParams.username = '';
                this.rowParams.costType = '';
                this.rowParams.limitAmt = null;
                this.rowParams.edit = true
                this.modalTitle = '新增'
                this.isModalOpen = true;
            },
            edit(params) {
                this.rowParams = params
                this.rowParams.username = this.rowParams.user
                this.rowParams.edit = false
                this.modalTitle = '编辑'
                this.isModalOpen = true;
            },
            closeModal() {
                this.rowParams.id = '';
                this.isModalOpen = false;
                this.formValidate.pageNumber = 1;
                this.formValidate.pageSize = 10;
                this.getList();
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "是否确认删除？",
                    onOk: () => {
                        deleteExemptConfig(v.id).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.getList();
                            }
                        });
                    }
                });
            },
        },

    };
</script>
